package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.atguigu.ggkt.vod.service.TeacherService;
import com.atguigu.ggkt.model.vod.Teacher;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author sleephardzz
 * @since 2023-06-12
 */
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping(value="/admin/vod/teacher")
//@CrossOrigin//解决跨域问题
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //根据id查询讲师信息
    @ApiOperation("根据id查询讲师信息")
    @GetMapping("inner/getTeacher/{id}")
    public Teacher getTeacherInfo(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return teacher;
    }

    /**
     * http://localhost:8301/admin/vod/teacher/findAll
     * 查询所有讲师
     * @return
     */
    @ApiOperation("所有讲师列表")
    @GetMapping("findAll")
    public Result<List<Teacher>> findAllTeacher(){
        List<Teacher> list = teacherService.list();
        return Result.ok(list).message("查询所有讲师");
    }
    /**
     * 删除讲师
     * @return
     */
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("remove/{id}")
    public Result removeTeacher(
            @ApiParam(name = "id", value = "主键id", required = true) @PathVariable(required = true) Long id){
        boolean success = teacherService.removeById(id);
        if (success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @ApiOperation("条件查询+分页")
    @PostMapping("findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) TeacherQueryVo teacherQueryVo){
        Page<Teacher> pageParam = new Page<>(current, limit);

        //没任何条件：直接查询所有
        if (teacherQueryVo==null) {
            IPage<Teacher> page = teacherService.page(pageParam, null);
            return  Result.ok(page);
        }else {
            //获取条件值
            String name = teacherQueryVo.getName();//讲师名称
            Integer level = teacherQueryVo.getLevel();//讲师级别
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();//开始时间
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();//结束时间
            //封装条件
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(name)) {
                wrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(level)) {
                wrapper.eq("level",level);
            }
            if(!StringUtils.isEmpty(joinDateBegin)) {
                //大于等于
                wrapper.ge("join_date",joinDateBegin);
            }
            if(!StringUtils.isEmpty(joinDateEnd)) {
                //小于等于
                wrapper.le("join_date",joinDateEnd);
            }
            IPage<Teacher> page = teacherService.page(pageParam);
            return  Result.ok(page);
        }
    }

    @ApiOperation("添加讲师")
    @PostMapping("saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean success = teacherService.save(teacher);
        if (success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @ApiOperation("根据id查询")
    @PostMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody  Teacher teacher){
        boolean success = teacherService.updateById(teacher);
        if (success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @ApiOperation("批量删除讲师")
    @PostMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> ids){
        boolean success = teacherService.removeByIds(ids);
        if (success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }


}


